//
//  ViewController.m
//  12-gcd常用函数
//
//  Created by J on 16/2/25.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
//    Person* p = [Person new];
//    
//    NSLog(@"%@",p.books);
//    Person* p2 = [Person new];
//    NSLog(@"%@",p2.books);

    [self barrier];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
//    NSFileManager * manager = [NSFileManager defaultManager];
//    NSString* to = @"/Users/J/Desktop/未命名文件夹";
//    NSString* from = @"/Users/J/Desktop/To";
//    NSArray* subPathes =  [manager subpathsAtPath:@"/Users/J/Desktop/To"];
//
//    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_apply(subPathes.count, queue, ^(size_t index) {
//        
//        NSString* fromFullPath = [from stringByAppendingPathComponent:subPathes[index]];/////Users/J/Desktop/未命名文件夹多个///会被合并为/ 如果没有就会增加一个
////                NSLog(@"%@",fromFullPath);
//        NSString * toFullString = [to stringByAppendingPathComponent:subPathes[index]];
////                    NSLog(@"%@",toFullString);
//        [manager moveItemAtPath:fromFullPath toPath:toFullString error:nil];//剪切文件
//        
//    });
    
 

    
    
    
//    for (NSUInteger i = 0; i<10; i++) {
//        NSLog(@"-------------------%zd",i);
        //并发队列
//        dispatch_apply(10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
//            
//            NSLog(@"%zd %@",index,[NSThread currentThread]);
//            
//        });
//    }
    
    
    
    
  //    [self performSelector:@selector(run) withObject:nil afterDelay:2];
//    [self barrier];
//    dispatch_queue_t queue = dispatch_queue_create("com.jzj", NULL);
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), queue, ^{//也可以传一个自己的队列进去 通过  //dispatch_queue_get_label(queue)获取当前队列的标识符获取队列的标识符
    
//    });//默认主队列中执行
    
//    dispatch_async(queue, ^{
//       
//        NSLog(@"%@ %s",[NSThread currentThread],dispatch_queue_get_label(queue));//获取当前线程的名字
//    });
//
//    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
  
//    static dispatch_once_t onceToken;//整个程序运行过程中只会执行一次 可以用来做资源加载 懒加载不行(因为第二个对象需要这个属性的时候就直接返回nil了无法再创建对象)
//    dispatch_once(&onceToken, ^{//onceToken用来记录有没有执行过这个代码
//        
//        NSLog(@"%@ ",[NSThread currentThread]);//这里面的东西是线程安全的不用担心多条线程同时抢夺资源
//    });

}

- (void)moveFile{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSFileManager * manager = [NSFileManager defaultManager];
        NSString* from = @"/Users/J/Desktop/未命名文件夹";
        NSString* to = @"/Users/J/Desktop/To";
        NSArray* subPathes =  [manager subpathsAtPath:@"/Users/J/Desktop/未命名文件夹"];
        for (NSString* subPath in subPathes) {
            NSString* fromFullPath = [from stringByAppendingPathComponent:subPath];/////Users/J/Desktop/未命名文件夹多个///会被合并为/ 如果没有就会增加一个
            //        NSLog(@"%@",fromFullPath);
            NSString * toFullString = [to stringByAppendingPathComponent:subPath];
            
//            NSLog(@"%@",toFullString);
            [manager moveItemAtPath:fromFullPath toPath:toFullString error:nil];//剪切文件
            
        }
        //    NSLog(@"%@",subPathes);//包含当前文件的所有路径 里面子文件夹的路径也会包含
    });
    
    

    
}

-(void)run{
    NSLog(@"run");
}
- (void)barrier{
    for (int i = 0; i<1; i++) {
        
        dispatch_queue_t queue = dispatch_queue_create("", DISPATCH_QUEUE_CONCURRENT);
        //        dispatch_queue_t queue = dispatch_get_main_queue();
        
        dispatch_async(queue, ^{
            
            NSLog(@"1%@",[NSThread currentThread]);
        });
        dispatch_async(queue, ^{
            
            NSLog(@"2%@",[NSThread currentThread]);
        });
        dispatch_barrier_sync(queue, ^{
            
            NSLog(@"dispatch_barrier_async");
        });
        dispatch_async(queue, ^{
            
            NSLog(@"3%@",[NSThread currentThread]);
        });
        
        dispatch_async(queue, ^{
            
            NSLog(@"4%@",[NSThread currentThread]);
        });
        
        
    }
    
    NSLog(@"常用常用常用常用常用常用常用常用常用常用常用常用常用常用常用");
    
    

}

@end
